﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EcSalseManage.PageCommon;
using EcSalseManage.App_Code;

namespace EcSalseManage.EcSalse
{
    public partial class Report_Plan_Detail : System.Web.UI.Page
    {



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!CommonFunction.isInPrev("Report_Plan_0001"))
                {
                    Response.Redirect("../Error/Perm_Error.aspx");
                }

                if (!CommonFunction.isInPrev("Report_Plan_0002"))
                {
                    this.btnSave.Enabled = false;
                }

                if (!CommonFunction.isInPrev("Report_Plan_0003"))
                {
                    this.btnDelete.Enabled = false;
                }

                if (Request.QueryString["id"] != null)
                {
                    ViewState["id"] = Request.QueryString["id"];
                    Update_Data_Bind(ViewState["id"].ToString());
                    this.btnDelete.Visible = true;
                    this.btnReset.Visible = false;
                    this.Report_Tab1.Visible = true;
                    this.Report_Tab1.MainID = ViewState["id"].ToString();
                }
                else if (Session["ProjectID"] != null)
                {
                    DataAccess.Operate.DA_Project_Inherit Project_Inherit = new DataAccess.Operate.DA_Project_Inherit();
                    this.lblProjectName.Text = Project_Inherit.getProjectName(Session["ProjectID"].ToString());
                    this.btnAudit.Visible = false;
                    //id = "";  //如果没有ID参数传入，则是新增操作
                }

   

                if (!CommonFunction.isInPrev("Report_Plan_0005"))
                {
                    this.btnAudit.Enabled = false;
                }

            }

        }

        private void Update_Data_Bind(string id)
        {
            DataAccess.Operate.DA_Report_Plan_Operate Report_Plan_Operate = new DataAccess.Operate.DA_Report_Plan_Operate();
            DataEntity.T_Report_Plan Report_Plan = (DataEntity.T_Report_Plan)Report_Plan_Operate.SelectOne(id);
            txtReport_Plan_ExchangeDate.Text = Report_Plan.Report_Plan_ExchangeDate.ToString();
            txtReport_Plan_ReportNO.Text = Report_Plan.Report_Plan_ReportNO.ToString();
            txtReport_Plan_Comm.Text = Report_Plan.Report_Plan_Comm.ToString();
            txtReport_Plan_ProjectDep.Text = Report_Plan.Report_Plan_ProjectDep.ToString();
            txtReport_Plan_Referral.Text = Report_Plan.Report_Plan_Referral.ToString();
            txtReport_Plan_PayRemark.Text = Report_Plan.Report_Plan_PayRemark.ToString();
            txtReport_Plan_Remark.Text = Report_Plan.Report_Plan_Remark.ToString();

            if (Report_Plan.Report_Plan_Audit > 6)
            {
                this.btnSave.Enabled = false;
                this.btnDelete.Enabled = false;
            }

            if (Report_Plan.Report_Plan_Audit == 6)
                this.btnAudit.Text = "事总审核";
            if (Report_Plan.Report_Plan_Audit == 7)
            {
                this.btnAudit.Text = "审核已完成";
                this.btnAudit.Enabled = false;
            }

            if (!CommonFunction.isInPrev("Report_Plan_0005") && this.btnAudit.Text == "事总审核")
            {
                this.btnAudit.Enabled = false;
            }


            DataAccess.Operate.DA_Project_Inherit Project_Inherit = new DataAccess.Operate.DA_Project_Inherit();
            this.lblProjectName.Text = Project_Inherit.getProjectName(Report_Plan.Report_Plan_ProjectID.ToString());
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DataAccess.Operate.DA_Report_Plan_Operate Report_Plan_Operate = new DataAccess.Operate.DA_Report_Plan_Operate();
            DataEntity.T_Report_Plan Report_Plan = new DataEntity.T_Report_Plan();
            Report_Plan.Report_Plan_ExchangeDate = Convert.ToDateTime(this.txtReport_Plan_ExchangeDate.Text.Trim());
            Report_Plan.Report_Plan_ExchangeTypeID = 1;
            Report_Plan.Report_Plan_ReportNO = txtReport_Plan_ReportNO.Text.Trim();
            Report_Plan.Report_Plan_Comm = Convert.ToDecimal(txtReport_Plan_Comm.Text.Trim());
            Report_Plan.Report_Plan_ProjectDep = Convert.ToDecimal(txtReport_Plan_ProjectDep.Text.Trim());
            Report_Plan.Report_Plan_Referral = Convert.ToDecimal(txtReport_Plan_Referral.Text.Trim());
            Report_Plan.Report_Plan_PayRemark = txtReport_Plan_PayRemark.Text.Trim();
            Report_Plan.Report_Plan_Remark = txtReport_Plan_Remark.Text.Trim();
            Report_Plan.Report_Plan_Audit = 6;
            Guid gReport_ID = System.Guid.NewGuid();
            if (ViewState["id"]==null) //如果没有ID参数传入，则是新增操作
            {
                Report_Plan.Report_Plan_ID = gReport_ID;
                Report_Plan.Report_Plan_ProjectID = new Guid(Session["ProjectID"].ToString());
                if (Report_Plan_Operate.Insert(Report_Plan))
                {
                   
                    DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 1, gReport_ID, 1);
                    Response.Write("<script>alert('"+Common.Common_Const.MSG_ALL_INSERT_SUCCESS+"')</script>");
                    ClearForm();
                }
                else
                {
                    Response.Write("<script>alert('"+Common.Common_Const.MSG_ALL_INSERT_FAILS+"')</script>");
                }
            }
            else
            {
                Report_Plan.Report_Plan_ID = new Guid(ViewState["id"].ToString());
                if (Report_Plan_Operate.Update(Report_Plan))
                {
                    Response.Write("<script>alert('"+Common.Common_Const.MSG_ALL_UPDATE_SUCCESS+"')</script>");
                    DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 1, new Guid(ViewState["id"].ToString()), 2);
                }
                else
                {
                    Response.Write("<script>alert('"+Common.Common_Const.MSG_ALL_UPDATE_FAILS+"')</script>");
                }
            }

        }

        private void ClearForm()
        {
            Control frm = FindControl("Form1");
            foreach (Control ctrl in frm.Controls)
            {
                if (ctrl is TextBox)
                {
                    ((TextBox)ctrl).Text = " ";
                }
            }
        } 

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            DataAccess.Operate.DA_Report_Plan_Operate Report_Plan_Operate = new DataAccess.Operate.DA_Report_Plan_Operate();
            if (Report_Plan_Operate.Delete(ViewState["id"].ToString()))
            {
                Response.Write("<script>alert('"+Common.Common_Const.MSG_ALL_DELETE_SUCCESS+"');window.close();</script>");
                DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 1, new Guid(ViewState["id"].ToString()), 3);
            }
            else
            {
                Response.Write("<script>alert('"+Common.Common_Const.MSG_ALL_DELETE_FAILS+"');</script>");
            }
        }

        protected void btnAudit_Click(object sender, EventArgs e)
        {
            DataAccess.Operate.DA_Report_Plan_Inherit Report_Plan_Inherit = new DataAccess.Operate.DA_Report_Plan_Inherit();
            if (Report_Plan_Inherit.AuditByID(ViewState["id"].ToString(), "7"))
            {
                DataAccess.Multi.System_Multi.addLog(Session["EmployeeID"].ToString(), Session["EmployeeName"].ToString(), 6, new Guid(ViewState["id"].ToString()), 8);
                Response.Write("<script>alert('审核成功！');window.close();</script>");
            }
        }

        public void SendMessage(string purview, string project)
        {
            DataAccess.Operate.DA_Report_Plan_Inherit Report_Plan_Inherit = new DataAccess.Operate.DA_Report_Plan_Inherit();
            DataTable dt = Report_Plan_Inherit.GetCodeAndName(purview, project);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string name = dt.Rows[i]["EmployeeName"].ToString();
                string code = dt.Rows[i]["Code"].ToString();
                EcSalseManage.App_Code.CommonFunction.SendMessage(name, code, "策划报费-事总审批", "测试");
            }
        }

    }
}
